package net.mikoo.seals.manage.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.biz.service.DevicesServices;
import net.mikoo.seals.biz.service.SealServices;
import net.mikoo.seals.biz.service.WorkflowService;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.model.Devices;
import net.mikoo.seals.model.DevicesQuery;
import net.mikoo.seals.model.Seal;
import net.mikoo.seals.model.SealQuery;
import net.mikoo.seals.model.Workflow;
import net.mikoo.seals.model.WorkflowQuery;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/seal")
public class SealController extends BaseController {

	Logger logger = LoggerFactory.getLogger(SealController.class);

	@Autowired
	private SealServices sealServices;

	@Autowired
	private DevicesServices devicesServices;
	
 
	@Autowired
	private WorkflowService workflowService;

	@RequestMapping("/list")
	public String list(SealQuery query, HttpServletRequest request, Model model) {
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		PageModel<Seal> pm = sealServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "seal/list";
	}

	@RequestMapping("/add")
	public String form(HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

		WorkflowQuery query = new WorkflowQuery();
		query.setCompanyId(companyId);
		List<Workflow> workflows = workflowService.getListByQuery(query);
		model.addAttribute("workflows", workflows);
		return "seal/add";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 绑定设备
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/binding")
	public String binding(Long sealId ,HttpServletRequest request,Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		
		
		SealQuery sealQuery = new SealQuery();
		sealQuery.setCompanyId(companyId);
		sealQuery.setSealId(sealId);
		Seal seal = sealServices.getInfoByQuery(sealQuery);
		
		model.addAttribute("seal", seal);
		
		DevicesQuery devicesQuery = new DevicesQuery();
		devicesQuery.setCompanyId(companyId);
		devicesQuery.setDevicesStatus(DevicesStatusEnum.WAIT_BIND.code());
		List<Devices> deviceses = devicesServices.getListByQuery(devicesQuery);
		model.addAttribute("deviceses", deviceses);
		
		return "seal/binding";
	}
	
	
	/**
	 * 模拟测试添加
	 * @param devices
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/binding/submit")
	@ResponseBody
	public Map<String,Object> bindingSubmit(Devices devices,HttpServletRequest request,Model model) {
		
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			devices.setCompanyId(companyId);
			
			sealServices.bindingDevices(devices);
			
			return result(SUCCESS, "绑定成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}

	@RequestMapping("/create")
	@ResponseBody
	public Map<String, Object> create(Seal seal, HttpServletRequest request, Model model) {
		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

			seal.setCompanyId(companyId);
			sealServices.save(seal);
			return result(SUCCESS, "添加成功");
		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}
	}

	@RequestMapping("/update")
	public String update(SealQuery query, HttpServletRequest request, Model model) {
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		Seal seal = sealServices.getInfoByQuery(query);
		model.addAttribute("seal", seal);
		return "/seal/update";
	}

	@RequestMapping("/modify")
	@ResponseBody
	public Map<String, Object> modify(Seal seal, HttpServletRequest request, Model model) {

		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			seal.setCompanyId(companyId);
			sealServices.modify(seal);
			return result(SUCCESS, "添加成功");
		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}

	}

	@RequestMapping("/info")
	public String info(SealQuery query, HttpServletRequest request, Model model) {

		if (query.getSealId() == null) {
			throw new NullPointerException("SealId is null");
		}
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		Seal seal = sealServices.getInfoByQuery(query);
		model.addAttribute("seal", seal);

		return "seal/info";
	}

	/**
	 * 设备列表
	 * 
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/devicesList")
	public String devicesList(DevicesQuery query, HttpServletRequest request, Model model) {

		if (query.getSealId() == null) {
			throw new NullPointerException("SealId is null");
		}
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);

		query.setSealId(query.getSealId());
		query.setCompanyId(companyId);

		List<Devices> deviceses = devicesServices.getListByQuery(query);
		model.addAttribute("deviceses", deviceses);
		model.addAttribute("query", query);
		return "seal/devicesList";
	}
	
	/**
	 * 设备详情
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/devicesInfo")
	public String devicesInfo(DevicesQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		Devices devices = devicesServices.getInfoByQuery(query);
		model.addAttribute("devices", devices);
		return "seal/devicesInfo";
	}

	

	@RequestMapping("/changeStatus")
	@ResponseBody
	public Map<String, Object> changeStatus(Seal seal, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		seal.setCompanyId(companyId);

		try {
			sealServices.changeStatus(seal);
			return result(SUCCESS, "修改成功");
		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String, Object> getall(SealQuery query,HttpServletRequest request, Model model) {
		
		
		Map<String,Object> resut = new HashMap<>();
		
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			List<Seal> sels = sealServices.getListByQuery(query);
			
			
			JSONArray data = new JSONArray();
			if(sels !=null && !sels.isEmpty()) {
				
				for (Seal seal : sels) {
					JSONObject obj = new JSONObject();
					obj.put("name", seal.getSealName());
					obj.put("value", seal.getSealId());
					obj.put("selected", "");
					obj.put("disabled", "");
					data.add(obj);
				}
			}
			

			resut.put("code", 0);
			resut.put("msg", "success");
			resut.put("data", data);
		}catch (ParametersException e) {
			logger.error("", e);
			resut.put("code", -1);
			resut.put("msg", "error");
		}catch (Exception e) {
			logger.error("", e);
			resut.put("code", -1);
			resut.put("msg", "error");
		}
		
		return resut;
		
		
		
	}
	
	@RequestMapping("/setInterim")
	@ResponseBody
	public Map<String, Object> setInterim(Seal seal,HttpServletRequest request, Model model) {
		
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		seal.setCompanyId(companyId);

		try {
			devicesServices.setInterim(seal);
			return result(SUCCESS, "设置成功");
		} catch (ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return result(ERROR, "系统出错");
		}
		
		
	}
	
	

}